package com.itheima.health.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Slf4j
@DubboService
public class PermissionServiceImpl implements PermissionService{
	@Autowired
	PermissionMapper permissionMapper;


	@Override
	public List<Permission> findAll() {
		List<Permission> permissionList = permissionMapper.selectAll();
		return permissionList;
	}
	@Override
	public void add(Permission permission) {
		log.info("[权限---新增权限]data: {}",permission);
		permissionMapper.insertSelective(permission);

	}

	@Override
	public void delete(Integer id) {
		log.info("[权限---删除]data: {}",id);
		permissionMapper.deleteByPrimaryKey(id);

	}

	@Override
	public void edit(Permission permission) {
		log.info("[权限---修改]data: {}");
		permissionMapper.updateByPrimaryKeySelective(permission);

	}

	@Override
	public Permission findById(Integer id) {
		log.info("[权限---根据id查询权限]data: {}",id);
		return permissionMapper.selectByPrimaryKey(id);
	}

	@Override
	public PageResult findPage(QueryPageBean queryPageBean) {
		PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
		String queryString = queryPageBean.getQueryString();
		Example example = new Example(Menu.class);
		Example.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(queryString)) {
			criteria.orLike("code","%"+queryString+"%");
			criteria.orLike("name","%"+queryString+"%");

		}
		Page<Permission> page = (Page<Permission>)permissionMapper.selectByExample(example);
		List<Permission> permissionList = page.getResult();
		long total = page.getTotal();
		return new PageResult(total,permissionList);
	}

	@Override
	public Permission findcheckitemIdsById(Integer checkItemId) {
		CheckItem checkItem = new CheckItem();
		log.info("[菜单---根据检查项id查询菜单项]data: {}",checkItem.getId());
		return permissionMapper.selectByPrimaryKey(checkItem.getId());
	}
}
